package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.INotificationBizService;
import com.kk.xhr.admin.controller.request.NotificationAddRequest;
import com.kk.xhr.admin.controller.request.NotificationModifyRequest;
import com.kk.xhr.admin.controller.vo.NotificationVO;
import com.kk.xhr.common.pojo.AuthUser;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 通知管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/19
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "通知管理", tags = "通知管理")
@RequestMapping("/notification")
public class NotificationController {
    private final Executor adminControllerBizExecutor;
    private final INotificationBizService notificationBizService;

    @ApiOperation(value = "查询我的通知")
    @GetMapping("mine")
    public CompletionStage<ApiResult<List<NotificationVO>>> queryMine() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(notificationBizService.query(userId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "通知已阅读")
    @PutMapping("/read")
    public CompletionStage<ApiResult<Void>> readMessage(@RequestBody NotificationModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            notificationBizService.modifyStatus(request.getIds());
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加消息通知")
    @RequiresRoles(value = {"admin", "leader"}, logical = Logical.OR)
    @PostMapping
    public CompletionStage<ApiResult<Void>> readMessage(@RequestBody @Valid NotificationAddRequest request) {
        AuthUser authUser = ShiroUtil.getAuthUser();
        return CompletableFuture.supplyAsync(() -> {
            notificationBizService.add(authUser, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
